package com.sgb.knowledgeshare.view.fragment;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sgb.knowledgeshare.utils.OnVideoControllerListener;
import com.sgb.knowledgeshare.weight.ShareDialog;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.FragmentAttentionBinding;
import com.sgb.knowledgeshare.utils.PageChangeEvent;
import com.sgb.knowledgeshare.utils.RxBus;
import com.sgb.knowledgeshare.view.activity.LabelDetailsActivity;
import com.sgb.knowledgeshare.viewmodel.AttentionModel;
import com.sgb.knowledgeshare.weight.AlivcVideoListView;
import com.sgb.knowledgeshare.weight.AlivcVideoPlayView;
import com.sgb.knowledgeshare.weight.CommentDialog;
import com.sgb.knowledgeshare.weight.ControllerView;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.model.entity.VideoBean;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.util.MToast;
import com.swgk.core.view.entity.VideoListEntity;

import java.util.List;

public class AttentionFragment extends AppLazyFragment {

    AttentionModel viewmode;
    private FragmentAttentionBinding binding;
    private boolean isLoadMore;
    private int page;
    private List<VideoBean> adapterList;
    private int pages;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_attention, container, false);
        viewmode = new ViewModelProvider(this).get(AttentionModel.class);
        viewmode.setActivity(AttentionFragment.this);
        View decor = getActivity().getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        binding.videoPlayDetailView.setShowComment(false);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        binding.videoPlayDetailView.setOnRefreshDataListener(new AlivcVideoListView.OnRefreshDataListener() {
            @Override
            public void onRefresh() {
                initdata();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page++;
                if (page <= pages) {
                    viewmode.getFirstPageList(page + "", "1");
                }
            }
        });
        binding.videoPlayDetailView.setCallback(new AlivcVideoPlayView.UserVideoCallback() {
            @Override
            public void UserVideoController(ControllerView controller, VideoBean videoBean) {
                //视频右下角按钮点击事件处理
                likeShareEvent(controller);
            }

            @Override
            public void VideoInfo(int pos, VideoBean video) {
                //添加播放次数
                viewmode.getplayVideo(video.getVideoId());
            }

            @Override
            public void UserVideoLike(VideoBean bean, ControllerView controller) {
                //点赞
                viewmode.setLike(bean.getVideoId() + "");
                setShuangJiDianZan(bean, controller);
            }

        });
    }

    @Override
    protected void onLazyLoad() {
        initdata();
        binding.videoPlayDetailView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (binding != null) {
            binding.videoPlayDetailView.onPause();
        }
    }

    @Override
    public void onDestroyView() {
        binding.videoPlayDetailView.onDestroy();
        super.onDestroyView();
    }

    @Override
    public void onInvisible() {
        super.onInvisible();
        if (binding != null) {
            if (binding.videoPlayDetailView != null) {
                binding.videoPlayDetailView.onPause();
            }
        }
    }

    @Override
    public void onVisible() {
        super.onVisible();
        if (binding != null) {
            binding.videoPlayDetailView.onResume();
            if (binding.rlNodeta.getVisibility() == View.VISIBLE) {
                setView();
            }
        }
    }

    private void initdata() {
        isLoadMore = false;
        page = 1;
        viewmode.getFirstPageList(page + "", "1");
    }

    private void likeShareEvent(ControllerView controller) {
        controller.setListener(new OnVideoControllerListener() {
            //点赞
            @Override
            public void onLikeClick(VideoBean data) {
                viewmode.setLike(data.getVideoId() + "");
                setDianZan(data, controller);
            }

            //点击分享的事件处理
            @Override
            public void onShareClick(VideoBean videoData) {
                new ShareDialog(getActivity(), 1, videoData).setOnDeleteVideo(new ShareDialog.OnDeleteVideo() {
                    @Override
                    public void OnDeleteVideoData(int Id) {
                        DeleteVideo(Id);
                    }
                });
            }

            // 点击头像下面的+号事件处理
            @Override
            public void onFocusClick(VideoBean videoData) {

            }

            // 点击头像的事件处理
            @Override
            public void onHeadClick(VideoBean videoData) {
                RxBus.getDefault().post(new PageChangeEvent(1));
            }

            //评论
            @Override
            public void onCommentClick(VideoBean videoData) {
                new CommentDialog(getActivity(), videoData, "0", 1, 1).setonDialog(new CommentDialog.HiedDialog() {
                    @Override
                    public void onhiedDialog() {
                        viewmode.getCommentNum(videoData, controller);
                    }
                });
            }
            //直评功能
            @Override
            public void onCommentSend(VideoBean videoBean) {

            }
            //标签跳转功能
            @Override
            public void onLabelClickListener(VideoBean videoData) {
                LabelDetailsActivity.start(getActivity(),videoData.getLabelName(),videoData.getLabelNo());
            }
        });
    }

    //    删除视频的操作
    private void DeleteVideo(int id) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_message.setText("您的作品将被永久删除，无法找回。确认删除？");
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除视频的操作逻辑
                roundCornerDialog.dismiss();
                viewmode.deleteMyVideo(id);
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    private void setDianZan(VideoBean bean, ControllerView controller) {
        if (bean.isLiked()) {//为点赞
            int laudCountq = bean.getLaudCount();
            if (laudCountq != 0) {
                laudCountq--;
            }
            bean.setLiked(false);
            bean.setLaudCount(laudCountq);
        } else {
            int laudCountd = bean.getLaudCount();
            laudCountd++;
            bean.setLaudCount(laudCountd);
            bean.setLiked(true);
        }
        controller.like();
        controller.setVideoData(bean, getContext(),false);
    }

    private void setShuangJiDianZan(VideoBean bean, ControllerView controller) {
        if (bean.isLiked()) {
            int laudCountd = bean.getLaudCount();
            if (laudCountd > 0) {
                laudCountd--;
            }
            bean.setLaudCount(laudCountd);
            bean.setLiked(false);
        } else {
            int laudCountd = bean.getLaudCount();
            laudCountd++;
            bean.setLaudCount(laudCountd);
            bean.setLiked(true);
        }
        controller.like();
        controller.setVideoData(bean, getContext(),false);
    }


    public void setListVideo(VideoListEntity data) {
        List<VideoBean> records = data.getRecords();
        pages = data.getPages();
        if (null == records) {
            return;
        }
        if (isLoadMore) {
            if (records.size() > 0) {
                binding.videoPlayDetailView.addMoreData(records);
            } else {
                MToast.showToast(getActivity(), "没有更多视频啦！");
            }
        } else {
            if (records.size() > 0) {
                binding.videoPlayDetailView.refreshVideoList(records);
                binding.rlNodeta.setVisibility(View.GONE);
                binding.videoPlayDetailView.setVisibility(View.VISIBLE);
            } else {
                binding.rlNodeta.setVisibility(View.VISIBLE);
                binding.videoPlayDetailView.setVisibility(View.GONE);
                binding.videoPlayDetailView.loadFailure();
                setView();
            }
        }
        adapterList = binding.videoPlayDetailView.getAdapterList();
    }

    public void setCommentNum(VideoBean videoData, ControllerView controllerView) {
        controllerView.setVideoData(videoData, getContext(),false);
    }

    public void setDataList() {
        initdata();
    }

    public void setView() {
        binding.rlNodeta.setVisibility(View.VISIBLE);
        binding.videoPlayDetailView.setVisibility(View.GONE);
        View decor = getActivity().getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        ShareMianFragment fragment = (ShareMianFragment) getParentFragment();
        fragment.setTitleViews();
    }

}
